package com.vhr.controller;

import com.vhr.entities.Account;
import com.vhr.entities.Employee;
import com.vhr.entities.Result;
import com.vhr.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Name EmployeeController
 * @Description:
 * @Author: CHAO
 * @Date: 2019/9/26
 */
@Controller
@Api(description = "登录模块")
public class LoginController {

    @Autowired
    private IEmployeeService iEmployeeService;



    @GetMapping("/api/login")
    @ResponseBody
    @CrossOrigin
    public Result login(@RequestParam("username")Integer username, @RequestParam("password")String password, HttpSession session){
        Account account = new Account();
        account.setEmpNum(username);
        account.setEmpPassword(password);
        Employee loginUser = iEmployeeService.login(account);
        if(loginUser==null){
            return Result.fail("用户名或密码错误");
        }
        session.setAttribute("loginUser",loginUser);
        return Result.success().add("emp",loginUser);
    }

}
